package org.springService.test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyThread {
	
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		    //ArrayBlockingQueue<Runnable> arrayWorkQueue = new ArrayBlockingQueue<Runnable>(10);
	        LinkedBlockingDeque<Runnable> linkedWorkQueue = new LinkedBlockingDeque<Runnable>(5);
	        int count = 20;
	        ExecutorService threadPool = new ThreadPoolExecutor(2, //corePoolSize线程池中核心线程数
	                4, //maximumPoolSize 线程池中最大线程数
	                60, //线程池中线程的最大空闲时间，超过这个时间空闲线程将被回收
	                TimeUnit.SECONDS,//时间单位
	                //下面是采用有界队列和无界队列的区别
	                linkedWorkQueue,
	                //linkedWorkQueue,
	                //下面是jdk的四种执行策略
	                //new ThreadPoolExecutor.AbortPolicy()  这种策略直接抛出异常，丢弃任务。
	                //new ThreadPoolExecutor.DiscardPolicy() 这种策略和AbortPolicy几乎一样，也是丢弃任务，只不过他不抛出异常。
	                //new ThreadPoolExecutor.CallerRunsPolicy() //线程调用运行该任务的 execute 本身。此策略提供简单的反馈控制机制，能够减缓新任务的提交速度。没看明白，当时是我的main线程执行的task5
	                new ThreadPoolExecutor.DiscardOldestPolicy()//如果执行程序尚未关闭，则位于工作队列头部的任务将被删除，然后重试执行程序（如果再次失败，则重复此过程）
	        );
	        @SuppressWarnings("rawtypes")
			Callable myCallable = new Callable() {
    		    @Override
    		    public String call() throws Exception {
    		        Thread.sleep(3000);
    		        System.out.println("calld方法执行了");
    		        return "call方法返回值";
    		    }
    		};
	        for (int i = 1; i <= count ;i++){
	        	 
	            TestThreadPoolTask task = new TestThreadPoolTask(i,"name"+i);
	            //threadPool.execute(task);
	            @SuppressWarnings("unchecked")
				Future<?> future = threadPool.submit(task);
	            System.out.println("获取返回值: "+future.get());
	        }
	 
	        threadPool.shutdown();
	 
	    }

	}

class TestThreadPoolTask implements Runnable{
    private int i;
    private String name;
	public TestThreadPoolTask(int i, String name) {
		this.i = i;
		this.name = name;
	}

	@Override
	public void run() {
		System.out.println("---开始----"+name);
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("----完成OK---"+name);
	}
	
}